<?php
/**
 * Hello Model for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );
//jimport( 'joomla.application.component.helper' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TSBModelUsers extends JModel
{
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the hello identifier
	 *
	 * @access	public
	 * @param	int Hello identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	function &getAllData()
	{
		// Load all data
//		if (empty( $this->_data )) {
			$query = "SELECT " .
					"#__ju_tsb_users.system_user_id AS id, " .
					"#__users.username, " .
					"#__users.name, " .
					"#__users.usertype, " .
					"#__users.email, " .
					"#__ju_tsb_roles.id AS 'tsb_role_id', " .
					"#__ju_tsb_roles.name AS 'tsb_role' " .
					"FROM " .
					"#__ju_tsb_users " .
					"Inner Join #__users ON #__ju_tsb_users.system_user_id = #__users.id " .
					"Inner Join #__ju_tsb_roles ON #__ju_tsb_roles.id = #__ju_tsb_users.tsb_role_id " .
					"ORDER BY #__users.username";

			$this->_data = $this->_getList( $query );

			//$this->_db->setQuery( $query );
			//$this->_data = $this->_db->loadObjectList();
//		}
		if (!$this->_data) {
			$this->_data = null;
		}
		return $this->_data;
	}

	/**
	 * Method to get a hello
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = "SELECT " .
					"#__users.username, " .
					"#__users.name, " .
					"#__users.usertype, " .
					"#__users.email, " .
					"#__ju_tsb_roles.name as 'tsb_role', " .
					"#__ju_tsb_roles.id as 'tsb_role_id', " .
					"#__ju_tsb_users.system_user_id as 'id' " .
					"FROM " .
					"#__ju_tsb_users " .
					"Inner Join #__users ON #__ju_tsb_users.system_user_id = #__users.id " .
					"Inner Join #__ju_tsb_roles ON #__ju_tsb_roles.id = #__ju_tsb_users.tsb_role_id " .
					"WHERE #__ju_tsb_users.system_user_id = " . $this->_id;

			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->name = null;
			$this->_data->username = null;
			$this->_data->email = null;
			$this->_data->tsb_role = null;
			$this->_data->usertype = null;
		}
		return $this->_data;
	}

	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{
		$data = JRequest::get( 'post' );

		$query = "SELECT * FROM #__ju_tsb_users WHERE system_user_id = '". $data[id] ."'";
		$this->_db->setQuery( $query );
		$exists = $this->_db->loadObject();

		//Neu da co ban ghi nay trong table #__ju_tsb_users thuc hien UPDATE
		if($exists != null){
			$query = "UPDATE #__ju_tsb_users SET tsb_role_id = '" . $data[tsb_role_id] . "' " .
					"WHERE system_user_id = '" . $data[id] . "'";
			if (!$this->_db->execute($query)) {
				$this->setError( $row->getErrorMsg() );
				return false;
			}
		}//Neu chua co ban ghi nay trong table #__ju_tsb_users thuc hien INSERT
		else{
			$query = "INSERT INTO #__ju_tsb_users VALUES('" . $data[id] . "', '" . $data[tsb_role_id] . "')";
			if (!$this->_db->execute($query)) {
				$this->setError( $row->getErrorMsg() );
				return false;
			}
		}
		return true;
	}

	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		/*if(!$this->isDeleteable($cids)){
			$this->setError( JText::_('CAN NOT DELETE ADMINISTRATOR ACCOUNT') );
			return false;
		}*/

		$row =& $this->getTable();

		if (count( $cids ))
		{
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}
	/*
	function deleteTSBUser($id){
		$query = "UPDATE #__ju_tsb_users SET role_id = null";
	}
*/
	function getAllSystemUsers(){
		$query = "SELECT * " .
				"FROM " .
				"#__users " .
				"WHERE #__users.id NOT IN (SELECT #__ju_tsb_users.system_user_id FROM #__ju_tsb_users)";

		$this->_data = $this->_getList( $query );
		if (!$this->_data) {
			$this->_data = null;
		}
		return $this->_data;
	}

	/*
	 * Check if the user to be deleted is Administrator or not
	 * */
	function isDeleteable($id){
		$query = "SELECT r.is_super_admin " .
				"FROM jos_ju_tsb_users u Inner Join jos_ju_tsb_roles r ON r.id = u.tsb_role_id " .
				"WHERE jos_ju_tsb_users.system_user_id =  '".$id."' AND r.is_super_admin = '1'";

		return ! $this->_getListCount() > 0;
	}
}
